﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PoolGame2.Menus
{
    public class MenuManager : GameComponent
    {
        public enum eMenuTypes
        {
            MENU_NONE = 0,
            MENU_MAIN,
            MENU_PAUSE,
            MENU_SEARCH,
            MENU_HOST,
            MENU_SIGN_IN,
            MENU_LOBBY,
            MENU_CREDITS,
        };

        public Menu currentMenu = null;
        public Menu prevMenu = null;

        public List<Overlay> activeOverlays = new List<Overlay>();

        private MainMenu mainMenu = null;
        private PauseMenu pauseMenu = null;
        private GamerSignInMenu signinMenu = null;
        private ServerMenu serverMenu = null;
        private LobbyMenu lobbyMenu = null;
        private CreditsMenu creditsMenu = null;

        public MenuManager(PoolGame game)
            : base(game)
        {
            mainMenu = new MainMenu(game);
            pauseMenu = new PauseMenu(game);
            signinMenu = new GamerSignInMenu(game);
            serverMenu = new ServerMenu(game);
            lobbyMenu = new LobbyMenu(game);
            creditsMenu = new CreditsMenu(game);

            activeOverlays.Add(new PlayerPresencePanel(game));
            activeOverlays.Add(new NixonOverlay(game));
        }

        public void SwitchMenu(eMenuTypes menu)
        {
            if (menu == eMenuTypes.MENU_NONE)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = null;
            }
            else if (menu == eMenuTypes.MENU_MAIN && currentMenu != mainMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = mainMenu;
                currentMenu.OnOpen();
            }
            else if (menu == eMenuTypes.MENU_PAUSE && currentMenu != pauseMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = pauseMenu;
                currentMenu.OnOpen();
            }
            else if (menu == eMenuTypes.MENU_SIGN_IN && currentMenu != signinMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = signinMenu;
                currentMenu.OnOpen();
            }
            else if (menu == eMenuTypes.MENU_SEARCH && currentMenu != serverMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = serverMenu;
                currentMenu.OnOpen();
            }
            else if(menu == eMenuTypes.MENU_LOBBY && currentMenu != lobbyMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = lobbyMenu;
                currentMenu.OnOpen();
            }
            else if (menu == eMenuTypes.MENU_CREDITS && currentMenu != creditsMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = creditsMenu;
                currentMenu.OnOpen();
            }
        }

        public void PreviousMenu()
        {
            if (prevMenu == null)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = null;
            }
            else if (prevMenu == mainMenu )
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = mainMenu;
                currentMenu.OnOpen();
            }
            else if (prevMenu == pauseMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = pauseMenu;
                currentMenu.OnOpen();
            }
            else if (prevMenu == serverMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = serverMenu;
                currentMenu.OnOpen();
            }
            else if (prevMenu == lobbyMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = lobbyMenu;
                currentMenu.OnOpen();
            }
            else if (prevMenu == creditsMenu)
            {
                if (currentMenu != null)
                    currentMenu.OnClose();
                prevMenu = currentMenu;
                currentMenu = creditsMenu;
                currentMenu.OnOpen();
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Guide.IsVisible)
                return;

            if (currentMenu != null)
                currentMenu.Update(gameTime);

            foreach (Overlay overlay in activeOverlays)
            {
                if (overlay.Visible)
                    overlay.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            if( currentMenu != null )
                currentMenu.Draw(gameTime);

            foreach (Overlay overlay in activeOverlays)
            {
                if( overlay.Visible )
                    overlay.Draw(gameTime);
            }
        }
    }
}
